package ru.resumeService.web.servlet;

import ru.resumeService.model.Resume;
import ru.resumeService.service.ResumeService;
import ru.resumeService.service.impl.ResumeServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class ResumeList extends HttpServlet {
    String output;
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession(true);
        Boolean isLogin = (Boolean)session.getAttribute("isLogin");
        ResumeService resumeService = new ResumeServiceImpl();
        String fieldName = "id";
        String order = "asc";
        if (request.getParameter("fieldName") != null) {
            fieldName = request.getParameter("fieldName");
        }
        if (request.getParameter("order") != null) {
            order = request.getParameter("order");
        }
        int from = 0;
        int to = 15;
        int countOfResume;
        countOfResume = resumeService.getCountOfResume();
        try {
            from = Integer.parseInt(request.getParameter("from"));
            to = Integer.parseInt(request.getParameter("to"));
        } catch (NumberFormatException e) {
        }
        request.setAttribute("from", from);
        request.setAttribute("to", to);
        request.setAttribute("countOfResume", countOfResume);
        if (isLogin) {
            output = "Авторизация пройдена";
            try {
                List<String> headers = resumeService.getHeaders();
                if (headers != null) {
                    request.setAttribute("headers", headers);
                }
            } catch (SQLException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            List<Resume> resumes = resumeService.getResumesByField(fieldName, order, from, to);
            if (resumes != null) {
                request.setAttribute("resumes", resumes);
            }

        }
        request.setAttribute("output", output);
        RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/resumeList.jsp");
        if (dispatcher != null) {
            try {
                dispatcher.forward(request, response);
            } catch (ServletException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }


    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");

    }

}
